package com.sunnypower.rushfitapp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sunnypower.rushfitapp.Coachpart.CourseListAdapter;
import com.sunnypower.rushfitapp.entity.VReservecourse;
import com.sunnypower.rushfitapp.util.GetData;
import com.sunnypower.rushfitapp.ysq.GuiderActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class VipMainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_mainvip;
    private Button btn_mainuser;
    private Button btn_course;
    private Button btn_coach;
    private Button btn_manual;

    private List<VReservecourse> mCourseBeanList;
    private CourseListAdapter adapter;
    private ListView lv_course;

    private TextView tv_notice;
    private String uid;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            init();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            iniCourseData();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println("finanl:" + mCourseBeanList.toString());

        lv_course = findViewById(R.id.lv_course);
        adapter = new CourseListAdapter(VipMainActivity.this, mCourseBeanList);
        lv_course.setAdapter(adapter);
    }

    private void init() throws IOException {
        Intent intent = getIntent();
        uid = intent.getStringExtra("uid");
        System.out.println(uid);
        username = intent.getStringExtra("username");

        String data = GetData.doGet("http://120.24.24.141:10990/rushfit/Find1Notice2app");
        System.out.println(data);

        mCourseBeanList = new ArrayList<>();

        System.out.println(username);
        btn_mainvip = (Button) findViewById(R.id.btn_mainvip);
        btn_mainuser = (Button) findViewById(R.id.btn_mainuser);
        btn_course = (Button) findViewById(R.id.btn_course);
        btn_coach = (Button) findViewById(R.id.btn_coach);
        btn_manual = (Button) findViewById(R.id.btn_manual);
        tv_notice = findViewById(R.id.tv_notice);

        parseJsonWithJsonObject(data);

        //设置按钮的点击事件
        btn_mainvip.setOnClickListener(this);
        btn_mainuser.setOnClickListener(this);
        btn_course.setOnClickListener(this);
        btn_coach.setOnClickListener(this);
        btn_manual.setOnClickListener(this);
        tv_notice.setOnClickListener(this);
    }

    //解析json数据
    private void parseJsonWithJsonObject(String data) throws IOException {
        System.out.println("responseData：" + data);
        try {
            JSONObject jsonObject = new JSONObject(data);
            System.out.println("jsonObject：" + jsonObject);
            String title = jsonObject.getString("title");
            String context = jsonObject.getString("context");
            tv_notice.setText("【"+title+"】"+context);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }

    private void iniCourseData() throws JSONException {
        String data = GetData.doGet("http://120.24.24.141:10990/rushfit/ShowTop5Reservecourse2App");
        System.out.println(data);
        //解析json数据
        System.out.println("responseData：" + data);
        JSONArray CourseList = null;
        try {
            CourseList = new JSONArray(data);
            System.out.println("CourseList：" + CourseList);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        for (int i = 0; i < CourseList.length(); i++) {
            JSONObject jsoncourse = CourseList.getJSONObject(i);
            VReservecourse bean = new VReservecourse();
            bean.setName(jsoncourse.getString("name"));
            bean.setCoursename(jsoncourse.getString("coursename"));
            bean.setTime(jsoncourse.getString("time"));
            bean.setRate(jsoncourse.getString("rate"));
            bean.setRid(jsoncourse.getInt("rid"));
            mCourseBeanList.add(bean);
        }
    }

    @Override
    public void onClick(View v) {
        Toast toast;
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_mainvip:
                toast = onCardClick(getApplicationContext(), "已在当前页面", Toast.LENGTH_SHORT);
                toast.show();
                break;
            case R.id.btn_mainuser:
                intent = new Intent(VipMainActivity.this, UserMainActivity.class);
                intent.putExtra("uid", uid);
                intent.putExtra("username", username);
                //resultCode  1代表成功，0代表失败：
                setResult(1, intent);
                startActivity(intent);
                finish();
                toast = onCardClick(getApplicationContext(), "欢迎来到个人中心", Toast.LENGTH_SHORT);
                toast.show();
                break;
            case R.id.btn_course:
                intent = new Intent(VipMainActivity.this, CourseActivity.class);
                intent.putExtra("name", "");
                intent.putExtra("coursename", "");
                intent.putExtra("uid", uid);
                startActivity(intent);
                break;
            case R.id.btn_coach:
                intent = new Intent(VipMainActivity.this, CoachActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_manual:
                intent = new Intent(VipMainActivity.this, GuiderActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_notice:
                intent = new Intent(VipMainActivity.this, NoticeActivity.class);
                startActivity(intent);
                break;

        }
    }

    public Toast onCardClick(Context context, CharSequence text, int duration) {
        Toast toast = Toast.makeText(context, text, duration);
        toast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
        return toast;
    }
}
